﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace lumo.input
{
    /// <summary>
    /// Listener of Input Objects
    /// </summary>
    public class InputListener : IUpdatableOnce
    {
        private List<IInputObject> InputObjectList = new List<IInputObject>();
        private List<IInputObject> InputUpdateList = new List<IInputObject>();
        private List<IInputObject> PooledObjects   = new List<IInputObject>();

        // The propertie for frame ID
        private uint _FrameID = 0;
        /// <summary>Access for FrameID</summary>
        public uint FrameID { get { return _FrameID; } }

        /// <summary>
        /// Add an object
        /// </summary>
        /// <param name="Object">Object to wrap.</param>
        /// <param name="NeedUpdate">Do you need to update this object?</param>
        public void Add(IInputObject Object, bool NeedUpdate = true)
        {
            InputObjectList.Add(Object);
            if (NeedUpdate)
                InputUpdateList.Add(Object);
        }

        /// <summary>
        /// Clear the list
        /// </summary>
        public void Clear()
        {
            InputObjectList.Clear();
            InputUpdateList.Clear();
        }

        /// <summary>
        /// Update the Wrapper
        /// </summary>
        /// <param name="Game">The game object</param>
        public void Update(LumoComponent Game)
        {
            // Only called once per frame.
            if (_FrameID == Game.FrameID)
                return;
            _FrameID = Game.FrameID;

            // Update every key
            foreach (IInputObject obj in InputUpdateList)
                obj.Update(Game);

            // Clear the pooled objects.
            PooledObjects.Clear();

            // Check for triggered objects
            foreach (IInputObject obj in InputObjectList)
            {
                if (obj.Repeated)
                    PooledObjects.Add(obj);
            }
        }

        /// <summary>
        /// Get the objects.
        /// </summary>
        public List<IInputObject> GetKeys()
        {
            // Update every key
            return PooledObjects;
        }
    }
}
